
/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com" .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hh.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hh.droid.R;
import com.hh.listeners.OnActionBarClickListener;
import com.hh.ui.UiUtility;

/**
 * 
 * An action bar view with different features : add button , add title , add popupMenu etc..
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */
public class UiActionBar extends LinearLayout implements OnClickListener {

	private LinearLayout _myContainerActionsLeft;
	private LinearLayout _myContainerActionsRight;
	private OnActionBarClickListener _myListenerClick;
	private Context _myContext;


	public UiActionBar(Context pContext, AttributeSet pAttributes) {

		super(pContext, pAttributes);
		_myContext = pContext;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity) getContext()).getLayoutInflater().inflate(R.layout.ui_action_bar, this);
		_myContainerActionsLeft = (LinearLayout) findViewById(R.id.actionContainerLeft);
		_myContainerActionsRight = (LinearLayout) findViewById(R.id.actionContainerRight);
	}

	/**
	 * <br>
	 * Create an action bar button
	 * 
	 * @author WajdiHh
	 * @param id : used to handle the click action
	 * @param text : Displayed text to left of the icon
	 * @param resourceDrawable : icon resource
	 * @return Button :the created button
	 */
	public Button createActionChild(int id, String text, Drawable pDrawable) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;
		float lMargLeftDP=_myContext.getResources().getDimension(R.dimen.marginLeft_btn_actionbar);
		int lMargLeftPX=UiUtility.getPxFromDp(_myContext,lMargLeftDP);
		
		return createButton(id, text, pDrawable, lWidht,lMargLeftPX,0);
	}
	
	/**
	 * <br>
	 * Create an action bar button
	 * 
	 * @author WajdiHh
	 * @param id : used to handle the click action
	 * @param text : Displayed text to left of the icon
	 * @param resourceDrawable : icon resource
	 * @param pMarginRight : margin right
	 * @param pMarginLeft : margin left
	 * @return Button
	 */
	public Button createActionChild(int id, String text, Drawable pDrawable,int pMarginLeft,int pMarginRight) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;
		
		return createButton(id, text, pDrawable, lWidht,pMarginLeft,pMarginRight);
	}
	
	/**
	 * <br>
	 * Create an action bar button
	 * 
	 * @author WajdiHh
	 * @param id : used to handle the click action
	 * @param text : Displayed text to left of the icon
	 * @param resourceDrawable : icon resource
	 * @param pWidth : width of the button
	 * @return Button
	 */
	public Button createActionChild(int id, String text, Drawable pDrawable,int pWidth) {

		float lMargLeftDP=_myContext.getResources().getDimension(R.dimen.marginLeft_btn_actionbar);
		int lMargLeftPX=UiUtility.getPxFromDp(_myContext,lMargLeftDP);	
		return createButton(id, text, pDrawable, pWidth,lMargLeftPX,0);
	}
	/**
	 * <br>
	 * Create an action bar button
	 * 
	 * @author WajdiHh
	 * @param id : used to handle the click action
	 * @param text : Displayed text to left of the icon
	 * @param resourceDrawable : icon resource
	 * @param pWidth : width of the button
	 * @param pMarginRight : margin right
	 * @param pMarginLeft : margin left
	 * @return Button
	 */
	public Button createActionChild(int id, String text, Drawable pDrawable,int pWidth,int pMarginLeft,int pMarginRight) {

		return createButton(id, text, pDrawable, pWidth,pMarginLeft,pMarginRight);
	}
	
	/**
	 * <br>
	 * Create an action bar title
	 * 
	 * @author WajdiHh
	 * @param text : title
	 * @return TextView
	 */
	public TextView createActionBarTitle(String text){

		TextView lTextView = new TextView(getContext());
		lTextView.setText(text);
		lTextView.setTextColor(Color.BLACK);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

		float lMargLeftDP=_myContext.getResources().getDimension(R.dimen.marginLeft_btn_actionbar);
		int lMargLeftPX=UiUtility.getPxFromDp(_myContext,lMargLeftDP);		
		lp.setMargins(0, 0,lMargLeftPX, 0);

		lp.gravity=Gravity.CENTER_VERTICAL;
		lTextView.setLayoutParams(lp);
		return lTextView;
	}

	/**
	 * <br>
	 * Create an action bar title
	 * 
	 * @author WajdiHh
	 * @param text : title
	 * @return TextView
	 * @param pMarginRight : margin right
	 * @param pMarginLeft : margin left
	 */
	public TextView createActionBarTitle(String text,int pMarginLeft,int pMarginRight){

		TextView lTextView = new TextView(getContext());
		lTextView.setText(text);
		lTextView.setTextColor(Color.BLACK);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);		
		lp.setMargins(pMarginLeft, 0,pMarginRight, 0);

		lp.gravity=Gravity.CENTER_VERTICAL;
		lTextView.setLayoutParams(lp);
		return lTextView;
	}
	
	/**
	 * Add action child view to the right side
	 * 
	 * 
	 * @param child : view to add
	 */
	public void addChildActionRight(View child) {
		_myContainerActionsRight.addView(child);

	}

	/**
	 * Add action child button  to the right side directly without build it 
	 * 
	 * @param id : id used to handle action on the button
	 * @param text : text displayed to left of the icon
	 * @param pDrawable : the icon
	 * @param pMarginRight : margin right
	 * @param pMarginLeft : margin left
	 */
	public void addChildActionRight(int id, String text, Drawable pDrawable,int pMarginLeft,int pMarginRight) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;		
		Button lBtn= createButton(id, text, pDrawable, lWidht,pMarginLeft,pMarginRight);
		_myContainerActionsRight.addView(lBtn);
	}

	/**
	 * <br>
	 * add action button on the right side of the action bar
	 * 
	 * @author WajdiHh
	 * @param id : id used to handle action on the button
	 * @param text : text displayed to left of the icon
	 * @param pDrawable : the icon
	 */
	public void addChildActionRight(int id, String text, Drawable pDrawable) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;
		float lMargLeftDP=_myContext.getResources().getDimension(R.dimen.marginLeft_btn_actionbar);
		int lMargLeftPX=UiUtility.getPxFromDp(_myContext,lMargLeftDP);
		
		Button lBtn= createButton(id, text, pDrawable, lWidht,lMargLeftPX,0);
		_myContainerActionsRight.addView(lBtn);

	}
	/**
	 * <br>
	 * add action button on the left side of the action bar
	 * 
	 * @author WajdiHh
	 * @param id : id used to handle action on the button
	 * @param text : text displayed to left of the icon
	 * @param pDrawable : the icon
	 */
	
	public void addChildActionLeft(int id, String text, Drawable pDrawable) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;
		float lMargLeftDP=_myContext.getResources().getDimension(R.dimen.marginLeft_btn_actionbar);
		int lMargLeftPX=UiUtility.getPxFromDp(_myContext,lMargLeftDP);
		
		Button lBtn= createButton(id, text, pDrawable, lWidht,lMargLeftPX,0);
		_myContainerActionsLeft.addView(lBtn);

	}
	
	/**
	 * <br>
	 * add action button on the left side of the action bar
	 * 
	 * @author WajdiHh
	 * @param id : id used to handle action on the button
	 * @param text : text displayed to left of the icon
	 * @param pDrawable : the icon
	 * @param pMarginRight : margin right
	 * @param pMarginLeft : margin left
	 */
	public void addChildActionLeft(int id, String text, Drawable pDrawable,int pMarginLeft,int pMarginRight) {

		int lWidht=LinearLayout.LayoutParams.WRAP_CONTENT;		
		Button lBtn= createButton(id, text, pDrawable, lWidht,pMarginLeft,pMarginRight);
		_myContainerActionsLeft.addView(lBtn);

	}
	/**
	 * <br>
	 * Add action child view to the left side
	 * 
	 * @author WajdiHh
	 * @param child : view to add
	 */
	public void addChildActionLeft(View child) {
		_myContainerActionsLeft.addView(child);

	}

	/**
	 * <br>
	 * Register a callback to be invoked when action child is clicked
	 * 
	 * @author WajdiHh
	 * @param v : onActionBarClickListener
	 * @see OnActionBarClickListener
	 */
	public void setOnActionBarClickListener(OnActionBarClickListener pListener) {
		_myListenerClick = pListener;
	}
	@Override
	public void onClick(View v) {

		if(_myListenerClick!=null)
			_myListenerClick.onActionBarClick(v.getId());

	}

	private Button createButton(int id, String text, Drawable pDrawable,int pWidth,int pMarginLeft,int pMarginRight){

		Button lButton = new Button(getContext());
		lButton.setId(id);
		lButton.setText(text);
		lButton.setTextColor(Color.BLACK);

		lButton.setBackgroundResource(R.drawable.selector_button);
		lButton.setCompoundDrawablesWithIntrinsicBounds(pDrawable, null, null, null);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(pWidth, LinearLayout.LayoutParams.WRAP_CONTENT);

		lp.setMargins(pMarginLeft, 0,pMarginRight, 0);
		lp.gravity=Gravity.CENTER_VERTICAL;
		lButton.setLayoutParams(lp);
		lButton.setOnClickListener(this);

		return lButton;
	}
}
